package com.ruoyi.client.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.client.pojo.form.FavoriteAddForm;
import com.ruoyi.client.pojo.vo.FavoriteVo;
import com.ruoyi.client.service.FavoriteService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Anonymous
@CrossOrigin
@RestController("ClientFavoriteController")
@Api(value = "v1", tags = "06.收藏模块")
@RequestMapping("/favorite")
public class FavoriteController {

    @Resource
    private FavoriteService favoriteService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增收藏", notes = "收藏商品")
    @PostMapping("/add")
    public R<Void> add(@RequestBody @Valid FavoriteAddForm form) {
        return favoriteService.add(form) ? R.ok() : R.fail();
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "取消收藏", notes = "根据收藏id取消")
    @ApiImplicitParam(name = "id", value = "收藏id", dataType = "Long", required = true)
    @DeleteMapping("/{id}")
    public R<Void> deleteById(@PathVariable("id") Long id) {
        return favoriteService.deleteById(id) ? R.ok() : R.fail();
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "根据商品取消收藏", notes = "根据商品id取消收藏")
    @ApiImplicitParam(name = "productId", value = "商品id", dataType = "Long", required = true)
    @DeleteMapping("/deleteByProduct/{productId}")
    public R<Void> deleteByProduct(@PathVariable("productId") Long productId) {
        return favoriteService.deleteByProductId(productId) ? R.ok() : R.fail();
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "清空我的收藏", notes = "删除当前用户所有收藏")
    @DeleteMapping("/deleteAllByMy")
    public R<Void> deleteAllByMy() {
        return favoriteService.deleteAllByMy() ? R.ok() : R.fail();
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "查询我的收藏列表", notes = "无参数")
    @GetMapping("/listByMy")
    public R<List<FavoriteVo>> listByMy() {
        return R.ok(favoriteService.listByMy());
    }

    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "判断商品是否收藏", notes = "返回true/false")
    @ApiImplicitParam(name = "productId", value = "商品id", dataType = "Long", required = true)
    @GetMapping("/isFavorite/{productId}")
    public R<Boolean> isFavorite(@PathVariable("productId") Long productId) {
        return R.ok(favoriteService.isFavorite(productId));
    }
}

